package com.zhoug.mpchart.formatters;

import android.util.Log;

import com.github.mikephil.charting.components.AxisBase;
import com.zhoug.mpchart.MPChartConfig;

import java.util.List;

/**
 * 文字轴标签转化器
 *
 * @Author 35574
 * @Date 2020/9/22
 * @Description
 */
public class MPAxisTextValueFormatter extends MPAbsValueFormatter {
    private static final String TAG = ">>>AxisValueFormatter";
    private static final boolean DEBUG = MPChartConfig.DEBUG;
    public static final int DEFAULT_MIN_VALUE = 1;
    /**
     * 轴标签集合
     */
    private List<String> valueTexts;
    /**
     * 排名
     */
    private List<String> rinking;
    /**
     * 数据Entry中x的最小值,设置0时第一个数据会在轴上
     */
    private int minValue = DEFAULT_MIN_VALUE;

    public MPAxisTextValueFormatter() {

    }

    protected int getPosition(float value) {
        if (valueTexts == null || valueTexts.isEmpty()) {
            return -1;
        }
        int position = (int) value - minValue;
        if (DEBUG) {
            Log.d(TAG, "getPosition:value=" + value + ",position=" + position);
        }
        if (position < 0 || position >= valueTexts.size()) {
            position = -1;
        }
        return position;
    }

    public String getRinking(float value, AxisBase axis) {
        int position = getPosition(value);
        if(rinking!=null){
            if(position>=0 && position<rinking.size()){
                return rinking.get(position);
            }
            return null;
        }
        return position != -1 ? String.valueOf(position+1) : null;
    }


    @Override
    public String getFormattedValue(float value) {
        if (DEBUG) {
            Log.d(TAG, "getFormattedValue:value=" + value);
        }
        return super.getFormattedValue(value);
    }

    @Override
    public String getAxisLabel(float value, AxisBase axis) {
        return getAxisLabelLimit(value, axis);
//        return super.getAxisLabel(value, axis);
    }


    @Override
    public String getAxisLabelNotLimitLength(float value, AxisBase axis) {
        int position = getPosition(value);
        if (position != -1) {
            return valueTexts.get(position);
        }
        return "";
    }

    /**
     * 设置坐标轴的值
     * @param valueTexts
     */
    public void setValueTexts(List<String> valueTexts) {
        this.valueTexts = valueTexts;
    }

    public void setMinValue(int minValue) {
        this.minValue = minValue;
    }

    public int getMinValue() {
        return minValue;
    }

    /**
     * 设置排名
     * @param rinking
     */
    public void setRinking(List<String> rinking) {
        this.rinking = rinking;
    }

}
